﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Survey.Entity;
using Survey.DataAccessLayer.Data;

namespace Survey.DataAccessLayer
{
    public class QuestionFeedbackAccessor : Accessor<QuestionFeedback>
    {
        private QuestionOptionAccessor _optionAccessor;
        private QuestionAccessor _questionAccessor;
        public QuestionFeedbackAccessor() : base(new SurveyDbContext())
        {
            _optionAccessor = new QuestionOptionAccessor();
            _questionAccessor = new QuestionAccessor();
        }
        public QuestionFeedback GetQuestionFeedback(int id)
        {
            var q = Select(questionFeedback => questionFeedback.Id == id);
            return q.Single();
        }
        /// <summary>
        /// 选择题作答
        /// </summary>
        /// <param name="questionOptionId"></param>
        /// <returns></returns>
        public bool Answering(int questionOptionId)
        {
            QuestionOption questionOption = _optionAccessor.GetQuestionOption(questionOptionId);
            // 2是主观题类型
            if (questionOption.RefQuestion.Type != 2)
            {
                QuestionFeedback questionFeedback = new QuestionFeedback { QuestionOptionId = questionOptionId };
                Add(questionFeedback);
                Save();
                return true;
            }
            return false;
        }
        /// <summary>
        /// 主观题作答
        /// </summary>
        /// <param name="questionId"></param>
        /// <param name="describe"></param>
        /// <returns></returns>
        public bool Answering(int questionId, string describe)
        {
            Question question = _questionAccessor.GetQuestion(questionId);
            // 2是主观题
            if (question.Type == 2)
            {
                QuestionOption questionOption =
                    new QuestionOption { QuestionId = questionId, Describe = describe };
                _optionAccessor.Add(questionOption);
                QuestionFeedback questionFeedback = new QuestionFeedback { RefQuestionOption = questionOption };
                Add(questionFeedback);
                Save();
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取问题的回答列表
        /// </summary>
        /// <param name="question"></param>
        /// <returns></returns>
        public IEnumerable<QuestionFeedback> GetQuestionFeedbacks(Question question)
        {
            var returnValue = new List<QuestionFeedback>();
            returnValue.AddRange(
                Select(
                    questionFeedback => questionFeedback.RefQuestionOption.QuestionId == question.Id));
            return returnValue;
        }
        public IEnumerable<QuestionFeedback> GetQuestionFeedbacks(int questionId)
        {
            Question question = _questionAccessor.GetQuestion(questionId);
            return GetQuestionFeedbacks(question);
        }
    }
}
